<template>
  <div class="wxlogin-container">
    <h4 id="login_title">授权登录</h4>
    <p class="need_hide">您正在登录到{{ VITE_APP_TITLE }}，是否登录？</p>
    <button id="signin-btn" class="need_hide">确认登录</button>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const { VITE_APP_TITLE, VITE_APP_BASE_URL, VITE_APP_WX_OPEN_URL } = import.meta.env

onMounted(() => {
  const query = new URLSearchParams(location.search)
  if (!query.get('login_id')) {
    router.replace('/chat/new')
  }
  document.querySelector('#signin-btn').addEventListener('click', function () {
    document.querySelector('#login_title').innerText = '正在登录中...'
    document.querySelectorAll('.need_hide').forEach(node => (node.style.display = 'none'))
    const state = window.btoa(JSON.stringify({ i: query.get('login_id'), v: query.get('ref') }))
    const redirectUri = encodeURIComponent(`${VITE_APP_BASE_URL}/auth/getWxAccessToken`)
    window.open(
      `${VITE_APP_WX_OPEN_URL}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`
    )
  })
})
</script>

<style lang="less" scoped>
.wxlogin-container {
  height: 90vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;

  #login_title {
    font-size: 28px;
    color: #0099ff;
    margin-bottom: 30px;
  }

  .need_hide {
    color: #666666;
  }

  #signin-btn {
    display: block;
    width: 100%;
    height: 40px;
    background: #0099ff;
    color: #ffffff;
    border: 0;
    outline: none;
    max-width: 300px;
    margin: 30px auto 0;
    cursor: pointer;
    border-radius: 50px;
  }
}
</style>
